package edu.ynmd.bmi.action;

import edu.ynmd.bmi.model.Stuinfo;
import edu.ynmd.bmi.service.MangeService;
import edu.ynmd.bmi.vo.BMIForm;
import edu.ynmd.bmi.vo.Result;
import edu.ynmd.bmi.vo.SFZForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
public class PublicAction {

    @Autowired
    private MangeService mangeService;


    @PostMapping("getBMIValue")
    public Result getBMIVlaue(@RequestBody BMIForm bmiForm) throws Exception{
        Float resultf=bmiForm.getTz()/(bmiForm.getSg()*bmiForm.getSg());
       String resultstr ="";

       if(resultf<=18.4){
           resultstr="偏瘦";
       }
       else  if (resultf>=18.5&&resultf<=23.9){
           resultstr="正常";
       }
       else if (resultf>=24&&resultf<=27.9){
           resultstr="微胖";
       }
       else {
           resultstr="肥胖";
       }


        Result r=new Result();

       r.setState(resultstr);
       r.setValue(resultf);


       switch (resultstr){
           case "偏瘦":
               r.setSugguest("多吃点");
               break;
           case "正常":
               r.setSugguest("不要多吃，也别少吃");
               break;
           case "微胖":
               r.setSugguest("少吃点");
               break;
           case "肥胖":
               r.setSugguest("绝食");
               break;



       }



      return r;
    }



    @GetMapping("testCRUD")
    @ResponseBody
    public List<Stuinfo> testCRUD()throws Exception{

        Stuinfo s=new Stuinfo();

        s.setXm("王二");
        s.setSfzh("530102197408270010");
        s.setCsd("云南省玉溪市九龙池");
        s.setCsrq("19740827");
        s.setXb("男");
        s.setStuinfoid("450a85dd-6734-4506-a3bb-8f5f68d8b4b0");

      return   mangeService.getStuinfoList();

    }




    @PostMapping("getSfzInfo")
    @ResponseBody
    public List<Stuinfo> getSfzInfo(@RequestBody SFZForm sfzForm) throws Exception{

        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();
        System.out.println("接收到的姓名是"+xm);
        System.out.println("接收到的身份证号是"+sfzh);
        String sfbm=sfzh.substring(0,2);
        System.out.println("省份字符串是"+sfbm);
        String sfname=mangeService.getProvinceNameByCode(sfbm);
        System.out.println("省份名称是"+sfname);

        String citybm=sfzh.substring(0,4);
        System.out.println("州市字符串是"+citybm);
        String cityname=mangeService.getCityNameByCode(citybm);
        System.out.println("州市名称"+cityname);
        String areabm=sfzh.substring(0,6);
        System.out.println("区县字符串是"+areabm);
        String areaname=mangeService.getAreaNameByCode(areabm);
        System.out.println("区县名称是"+areaname);

        String csrqstr= sfzh.substring(6,14);
        System.out.println("出生日期是"+csrqstr);

        String xbbm=sfzh.substring(16,17);
        System.out.println("性别字符是"+xbbm);
        String xbname=mangeService.getSexNameByCode(xbbm);
        System.out.println("性别名称是"+xbname);


        Stuinfo s=new Stuinfo();

        s.setXm(xm);
        s.setSfzh(sfzh);
        s.setCsd(sfname+cityname+areaname);
        s.setCsrq(csrqstr);
        s.setXb(xbname);
        mangeService.addStuinfo(s);




     return mangeService.getStuinfoList();
    }



    @PostMapping("addStuinfo")
    @ResponseBody
    public Boolean addStuinfo(@RequestBody SFZForm sfzForm) throws Exception{

        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();
        System.out.println("接收到的姓名是"+xm);
        System.out.println("接收到的身份证号是"+sfzh);
        String sfbm=sfzh.substring(0,2);
        System.out.println("省份字符串是"+sfbm);
        String sfname=mangeService.getProvinceNameByCode(sfbm);
        System.out.println("省份名称是"+sfname);

        String citybm=sfzh.substring(0,4);
        System.out.println("州市字符串是"+citybm);
        String cityname=mangeService.getCityNameByCode(citybm);
        System.out.println("州市名称"+cityname);
        String areabm=sfzh.substring(0,6);
        System.out.println("区县字符串是"+areabm);
        String areaname=mangeService.getAreaNameByCode(areabm);
        System.out.println("区县名称是"+areaname);

        String csrqstr= sfzh.substring(6,14);
        System.out.println("出生日期是"+csrqstr);

        String xbbm=sfzh.substring(16,17);
        System.out.println("性别字符是"+xbbm);
        String xbname=mangeService.getSexNameByCode(xbbm);
        System.out.println("性别名称是"+xbname);


        Stuinfo s=new Stuinfo();

        s.setXm(xm);
        s.setSfzh(sfzh);
        s.setCsd(sfname+cityname+areaname);
        s.setCsrq(csrqstr);
        s.setXb(xbname);
       return mangeService.addStuinfo(s);


    }

    @PostMapping("updateStuinfo")
    @ResponseBody
    public Boolean updateStuinfo(@RequestBody SFZForm sfzForm) throws Exception{

        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();
        System.out.println("接收到的姓名是"+xm);
        System.out.println("接收到的身份证号是"+sfzh);
        String sfbm=sfzh.substring(0,2);
        System.out.println("省份字符串是"+sfbm);
        String sfname=mangeService.getProvinceNameByCode(sfbm);
        System.out.println("省份名称是"+sfname);

        String citybm=sfzh.substring(0,4);
        System.out.println("州市字符串是"+citybm);
        String cityname=mangeService.getCityNameByCode(citybm);
        System.out.println("州市名称"+cityname);
        String areabm=sfzh.substring(0,6);
        System.out.println("区县字符串是"+areabm);
        String areaname=mangeService.getAreaNameByCode(areabm);
        System.out.println("区县名称是"+areaname);

        String csrqstr= sfzh.substring(6,14);
        System.out.println("出生日期是"+csrqstr);

        String xbbm=sfzh.substring(16,17);
        System.out.println("性别字符是"+xbbm);
        String xbname=mangeService.getSexNameByCode(xbbm);
        System.out.println("性别名称是"+xbname);


        Stuinfo s=new Stuinfo();
        s.setStuinfoid(sfzForm.getStuinfoid()); //修改操作必须添加主键
        s.setXm(xm);
        s.setSfzh(sfzh);
        s.setCsd(sfname+cityname+areaname);
        s.setCsrq(csrqstr);
        s.setXb(xbname);
        return mangeService.updateStuinfo(s);//修改操作


    }


    @PostMapping("deleteStuinfo")
    @ResponseBody
    public Boolean deleteStuinfo(@RequestBody Stuinfo stuinfo) throws Exception{


        return mangeService.deleteStuinfo(stuinfo.getStuinfoid());
    }


    @PostMapping("getSingleStuinfo")
    @ResponseBody
    public Stuinfo getSingleStuinfo(@RequestBody Stuinfo stuinfo) throws Exception{
        return mangeService.getSingleStuinfo(stuinfo.getStuinfoid());
    }

    @GetMapping("getStuinfoList")
    @ResponseBody
    public List<Stuinfo> getStuinfoList() throws Exception{

        return mangeService.getStuinfoList();
    }



}
